package com.hdoit.ares.logical.official.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.official.dao.IOnlineApplyforDao;
import com.hdoit.ares.logical.official.model.OnlineApplyfor;
import com.hdoit.ares.logical.viewmodel.official.OnlineApplyforSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年01月05日 10:34:28
 * @comment
 */
@Component
public class OnlineApplyforDaoImpl extends SuperDao<OnlineApplyfor> implements IOnlineApplyforDao{

	@Override
	public List<OnlineApplyfor> queryList(OnlineApplyforSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
		finderExpression.select("*")
		                .from(OnlineApplyfor.class)
		                .addOrder("applyforDate", OrderEnum.desc);
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	private void appendFinder(OnlineApplyforSearch search, IFinderExpression finder) {
		
		if(search.getStartDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},applyforDate)>=0")
				  .setParam("startDate", search.getStartDate());
		}
		
		if(search.getEndDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},applyforDate)<=0")
				  .setParam("endDate", search.getEndDate());
		}
		
		
		if(!StringUtils.isEmpty(search.getAnswerState())){
			finder.andWhere("answerState like #{answerState}")
				  .setParam("answerState", "%"+search.getAnswerState()+"%");
		}
		
		if(!StringUtils.isEmpty(search.getPhone())){
			finder.andWhere("phone like #{phone}")
				  .setParam("phone", "%"+search.getPhone()+"%");
		}

		if(!StringUtils.isEmpty(search.getUserName())){
			finder.andWhere("userName like #{userName}")
				  .setParam("userName", "%"+search.getUserName()+"%");
		}
	}

	@Override
	public List<OnlineApplyfor> queryList(OnlineApplyforSearch search) {
		return queryList(search,null);
	}

	@Override
	public int queryCount(OnlineApplyforSearch search) {
        IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		
        finderExpression.select("count(id)")
        				.from(OnlineApplyfor.class);
		
		appendFinder(search,finderExpression);
		
		return queryCount(finderExpression, search);
	}

}